//管理后台Tab框架设计
var yunMain = new Object();
yunMain.skin = "dhx_skyblue";
yunMain.treeImgPath = basePP + "js/dhtmlx/dhtmlxTree/codebase/imgs/";
yunMain.tabbarImgPath = basePP + "js/dhtmlx/dhtmlxTabbar/codebase/imgs/";
yunMain.tabbarSkin = "dhx_blue";
yunMain.tabNumLimit = 7;
yunMain.pages = {};
yunMain.windows = {};
yunMain.winImgPath = basePP + "js/dhtmlx/dhtmlxWindows/codebase/imgs/";
var contentPath,dhxWins,changePwdWin;
// yunMain.create_tab = function(id, treeId, paramArray) {
yunMain.create_tab = function(id, node, paramArray) {
	var tabWidth = 120;
	var tabNum = 0;
	// 当打开标签数大于7个的时候 关闭第一个
	var firstid = null;
	for ( var i in yunMain.tabbar._tabs) {
		if (tabNum == 1) {
			firstid = i;
		}
		if (i == id) {
			continue;
		}
		tabNum++;
	}
//	if (firstid != null && tabNum > yunMain.tabNumLimit) {//超出限制后先关闭第一个任务窗口
//		yunMain.tabbar.removeTab(firstid);
//		delete yunMain.pages[firstid];
//	}
	if (tabNum - 1 > yunMain.tabNumLimit) {
		$func.alert("任务窗口太多，请先关闭一些!", "red");
		return;
	} else if (!yunMain.pages[id]) {
		// switch (treeId.getUserData(id, "type")) {
		if(node){
			switch (node.attr("mType")) {
				case "2":
					// var actionUrl=treeId.getUserData(id, "action");
					var actionUrl=node.attr("mUrl");
					if(actionUrl.indexOf('http')>=0){
						contentPath =actionUrl;
					}else{
						contentPath = basePath + actionUrl.substring(1,actionUrl.length);
					}
					contentPath = contentPath.replace(/#/g, '&');
					break;
			}
		}
        yunMain.tabbar.enableTabCloseButton(true);
		var welltitle = "控制台";
		if (id == 0) {
			//contentPath=basePath+"static/welcome.html";
			contentPath=basePath+"console";
			yunMain.tabbar.enableTabCloseButton(false);
			yunMain.tabbar.addTab(id, welltitle, tabWidth);
			yunMain.tabbar.enableTabCloseButton(true);
		} else {
			// yunMain.tabbar.addTab(id, treeId.getItemText(id), tabWidth);
			yunMain.tabbar.addTab(id, node.attr("mName"), tabWidth);
		}
		// 修正所有tab的位置
		$(".dhx_tab_element").each(function(index) {
			$(this).css('left', 110 * index + 'px !important');
		});
		// 鼠标移上去更换关闭图标
		$("div.dhx_tab_element img").hover(
				function() {
					$(this).attr('src',yunMain.tabbarImgPath + 'dhx_blue/close_hover.png');
				},
				function() {
					$(this).attr('src',yunMain.tabbarImgPath + 'dhx_blue/close.png');
		});
		if (paramArray && paramArray.length != 0) {
			for ( var i = 0; i < paramArray.length; i++) {
				var param = paramArray[i];
				if (contentPath.indexOf('?') == -1) {
					contentPath += '?' + param.name + '=' + param.value;
				} else {
					contentPath += '&' + param.name + '=' + param.value;
				}

			}
		}
		/*if (treeId.getUserData(id, "buttons")) {
			if (contentPath.indexOf('?') == -1) {
				contentPath += '?';
			} else {
				contentPath += '&';
			}
			contentPath += "btns=" + treeId.getUserData(id, "buttons");
		}*/
		yunMain.tabbar.setContentHref(id, contentPath);
		var win = yunMain.tabbar.cells(id);
		yunMain.tabbar.setTabActive(id);
		yunMain.pages[id] = win;
		// 保存位于tabbar最右边的tab id
		yunMain.pages.lastid = id;
	} else{
		yunMain.tabbar.setTabActive(id);
	}
}

yunMain.init = function() {
	// mainLayout
	yunMain.layout = new dhtmlXLayoutObject(document.body, "2U", yunMain.skin);
	yunMain.layout.cont.obj._offsetTop = 30;
	yunMain.layout.cont.obj._offsetHeight = -31;
	yunMain.layout.setSizes();
	yunMain.layout.items[0].setWidth(198);
	// 隐藏版块头部
	yunMain.layout.forEachItem(function(item) {
		item.hideHeader();
	});
	yunMain.layout.attachEvent("onPanelResizeFinish",function(){
		if(yunMain.layout.items[0].getWidth()>300){
			$func.alert("当前宽度已到达最大限制","red");
			yunMain.layout.items[0].setWidth(300);
		}
    });
	var j_LayoutSinglePoly = $("td.dhtmlxLayoutSinglePoly");
	j_LayoutSinglePoly.first().append("<div class=\"headerBottomWrap\"></div>");
	$("div.dhxcont_global_content_area").first().addClass("main-navMenu");
	$("div.dhxcont_global_content_area")[0].children[0].style.height = ($("div.dhxcont_global_content_area")[0].children[0].clientHeight - 39) + "px";

	yunMain.layout.items[0].attachObject("leftMenuObj");
	$("#leftMenuObj").parent().attr("style","position: relative;left: 0px;top: 0px;overflow-y: auto;width: 198px;height: 568px;overflow-x: hidden;");

	/*yunMain.tree2 = yunMain.layout.items[0].attachTree(0);
	yunMain.tree2.setSkin(yunMain.skin);
	//yunMain.tree2.setImagePath(yunMain.treeImgPath);
	yunMain.tree2.setIconSize(20, 20);
	yunMain.tree2.enableTreeLines(false);
	yunMain.tree2.enableThreeStateCheckboxes(true);
	//获得账户权限导航树
	$.ajax({
        url: basePath+'leftMenu',
        type: 'get',
        dataType: 'json',
        success: function (data) {
            if (!data || !data.result) {
                $func.alert(data.message?data.message:'获取菜单失败', "red");
                return;
            }
            yunMain.tree2.loadXMLString(data.content);
            yunMain.tree2.openAllItems(0);
        },
        error:function(xhr, textStatus, statusText){
            $func.alert('获取菜单请求失败:'+textStatus+":"+statusText, "green");
        }
    });
	// XML加载完成时修改tree样式
	yunMain.tree2.attachEvent("onXLE", function(tree, id) {
		$(".containerTableStyle>table").css('margin', '4px 0 50px');// 修改导航有时候被遮盖
		var $menuItem = $(".containerTableStyle>table>tbody>tr>td>table");
		$menuItem.each(function() {
			$(this).find("tr:first").addClass("navMenu-level1").nextAll("tr").addClass("navMenu-level2");
			//$(this).find("tr:first").find("td:eq(2)").css("display", "none");
			//$(this).find("tr:gt(0)").find(".standartTreeImage").end().find("td:gt(0)").find("td:eq(1)")
			//.css("display", "none");// 隐藏IE下navMenu-level2内第二个TD
		});
		yunMain.treeMouseEvent();// 鼠标hover事件
	});
	//点击导航菜单，创建tab
	yunMain.tree2.attachEvent("onClick", function(id) {
		yunMain.tree2.openItem(id);
		var $menuItemSelected, $menuItemSelPrev;
		$("td.standartTreeRow").removeClass("navMenuSelected");
		$menuItemSelected = $("span.selectedTreeRow").parents("td.standartTreeRow");
		$menuItemSelected.parents("table").addClass("w");
		$menuItemSelected.addClass("navMenuSelected");
		var level2 = yunMain.tree2.getLevel(id);
		if (level2 == 2)
			yunMain.create_tab(id, yunMain.tree2);
		return true;
	});*/
	
	yunMain.tabbar = yunMain.layout.items[1].attachTabbar();
	yunMain.tabbar.setHrefMode("iframes-on-demand");
	yunMain.tabbar.setSkin(yunMain.tabbarSkin);
	yunMain.tabbar.setImagePath(yunMain.tabbarImgPath);
	yunMain.tabbar.enableScroll(true);
	yunMain.tabbar.setMargin("0");
	yunMain.tabbar.setOffset("0");
	yunMain.tabbar.attachEvent("onTabClose", function(id) {
		var tabNum = 0;
		for ( var i in yunMain.pages)
			tabNum++;
		// tabbar只剩一个tab时此tab不能关闭，注：此功能发生修改，只剩最后一个tab也可以关闭
		if (id == yunMain.pages.lastid) {
			yunMain.tabbar.goToPrevTab();
			yunMain.pages.lastid = yunMain.tabbar.getActiveTab();
		} else {
			yunMain.tabbar.goToNextTab();
		}
		yunMain.tabbar.removeTab(id);
		delete yunMain.pages[id];

		$(".dhx_tab_element").each(function(index) {
			$(this).css('left', 110 * index + 'px !important');
		});
	});
	// XML加载完成时修改tabbar样式
	yunMain.tabbar.attachEvent("onXLE", function(id) {
;		$(".dhx_tab_element").first().addClass("tabbar-firstTab");
		//给相应的tab增加title内容
;		$(".dhx_tab_element_active span").first().attr("title",$(".dhx_tab_element_active span").first().html());
	});
	// 修正所有tab的位置
	$("div.dhx_tablist_zone").click(function() {
		$(".dhx_tab_element").each(function(index) {
			$(this).css('left', 110 * index + 'px !important');
		});
	});

	// 添加鼠标事件
	yunMain.treeMouseEvent = function() {
		// 设置第一层菜单鼠标移上去的样式
		$("td .standartTreeRow").mouseover(function() {
			$(this).css("background-position", "4px -34px");
		});
		// 设置第一层菜单鼠标移走后的样式
		$("td .standartTreeRow").mouseout(function() {
			$(this).css("background-position", "0 30px");
		});
	}
}
$(function() {
	yunMain.init();
	// yunMain.create_tab(0, yunMain.tree2);
	yunMain.create_tab(0, null);
	yunMain.layout.items[0].setWidth(198);
	$("#logout").click(function(){
		$func.confirm({
	   		content : "您确定要退出吗？",
	   		ok : function() {
	   			window.location = basePath+"loginOut";
	   		}
	   	});
		// $("#jbox-states").attr("style","height: 100px;");
	});
	$("#changePwd").click(function(){
		if(!dhxWins) dhxWins=new dhtmlXWindows();
		dhxWins.setImagePath(basePP + "js/dhtmlx/dhtmlxWindows/codebase/imgs/");
		changePwdWin=dhxWins.createWindow("changePwdWin", 20, 30, 325, 246);
		changePwdWin.center();
		changePwdWin.setModal(true);
		changePwdWin.bringToTop();
		changePwdWin.setText("<span style=\"font-size:14px;\">修改密码</span>");
		changePwdWin.denyResize();
		changePwdWin.button("minmax1").hide();
		changePwdWin.button("park").hide();
		changePwdWin.attachURL(basePath+"changePwdWin");
	});
	$('.dhtmlxLayoutPolySplitterVer div').css("width","3px");//分割线
});
